package com.dao.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class MybatisGenerateRun {

    public static void main(String[] args) throws IOException, XMLParserException, InvalidConfigurationException, SQLException, InterruptedException {
        List<String> warnings = new ArrayList<>();

        //加载数据库信息，例如driverClassName，username,password,url等
        Properties extraProperties = PropertiesLoaderUtils.loadAllProperties("jdbc.properties");
        ConfigurationParser cp = new ConfigurationParser(extraProperties, warnings);
        //ConfigurationParser cp = new ConfigurationParser(warnings);
        //加载generatorEmallConfig文件
        Configuration config = cp.parseConfiguration(new ClassPathResource("generatorConfig.xml").getFile());
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
        if(!CollectionUtils.isEmpty(warnings)){
            for (String warn : warnings) {
                System.out.println(warn);
            }
        }
        System.out.println("生成成功！");
    }

}
